import { createSlice } from "@reduxjs/toolkit";

const initialState = {
  username: "",
  userid: "",
  token: "",
}
const userSlice = createSlice({
  name: 'user',
  initialState,
  reducers: {
    userLogout: (state) => {
      state.username = "";
      state.userid = "";
      state.token = "";
    },
    userLogin: (state, action) => {
      // payload 为动作 userLogin 的传参，!!所以userLogin的传参需要为： {username,userid,token}
      state.username = action.payload.username;
      state.userid = action.payload.userid;
      state.token = action.payload.token;
    },
    userInfo: (state) => {
      return {
        username: state.username,
        userid: state.userid,
      }
    },
    userIsLogin: (state) => {
      return { token: state.token};
    }
  }
})

export const { userLogout, userLogin, userInfo, userIsLogin } = userSlice.actions;

export default userSlice.reducer;